package com.mrjy.wechat.controller;

import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.ConstantsUtil;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.maintain.bo.IUserInfoBo;
import com.mrjy.maintain.dao.IUserInfoDao;
import com.mrjy.wechat.bo.IWeChatFileUploadBo;
import com.mrjy.wechat.conts.WxConsts;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import java.util.HashMap;
import java.util.Map;

/**
 * 从微信服务器下载文件 controller类
 * @author csshui
 *
 */
@Controller
@RequestMapping("weChat/weChatFileUpload")
public class WeChatFileUploadController {
	
	@Resource
	private IWeChatFileUploadBo weChatFileUploadNo;
	@Resource
    private IUserInfoDao userInfoDao;

    /**
     * 上传头像
	 * @param userId
	 * @param mediaId
	 * @return
	 */
	@RequestMapping(value = "/uploadWXFile")
	@ResponseBody
	public Object uploadWXFile(HttpSession session,
			@RequestParam("mediaId") String mediaId) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> resultMap = null;
		UserInfo user = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if (null == user) {
			resultMap = new HashMap<String, Object>();
			resultMap.put("err_code", -2);
			resultMap.put("err_msg", "当前没有登录，请重新登录");
			return resultMap;
		}
		try {
			map.put("userId",user.getId());
			map.put("mediaId", mediaId);
			// 保存文件到服务器，并得到返回状态
			resultMap =  weChatFileUploadNo.uploadPmgWXFile(map);
			String path = session.getServletContext().getRealPath(WxConsts.ICO_SAVE_PATH);
			System.out.println("--------------------------------------------->path: "  + path);
			if (null != resultMap) {
				String picName = (String) resultMap.get("picName");
				
				if (null != user) {
					user.setPicName(picName);
					session.setAttribute(GlobalConstant.USER_INFO, user);
					
				}
			}

		} catch (Exception ex) {
			if (null == resultMap) {
				resultMap = new HashMap<String, Object>();
				resultMap.put("err_code", -1);
				resultMap.put("err_msg", "上传文件异常");
			}
		} 
		return resultMap;
	}

    /**
     * 上传头像
     *
     * @param userId
     * @param mediaId
     * @return
     */
    @RequestMapping(value = "/uploadWXFileOfFamilyPic")
    @ResponseBody
    public Object uploadWXFileOfFamilyPic(HttpSession session,
                                          @RequestParam("mediaId") String mediaId ) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> resultMap = null;
        UserInfo user = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
        if (null == user) {
            resultMap = new HashMap<String, Object>();
            resultMap.put("err_code", -2);
            resultMap.put("err_msg", "当前没有登录，请重新登录");
            return resultMap;
        }
        try {
            map.put("userId", user.getId());
            map.put("mediaId", mediaId);
            map.put("save_path", WxConsts.FAMILYPIC_SAVE_PATH);
            // 保存文件到服务器，并得到返回状态
            resultMap = weChatFileUploadNo.uploadPmgWXFile(map);
            String path = session.getServletContext().getRealPath(WxConsts.FAMILYPIC_SAVE_PATH);
            System.out.println("--------------------------------------------->path: " + path);

        } catch (Exception ex) {
            if (null == resultMap) {
                resultMap = new HashMap<String, Object>();
                resultMap.put("err_code", -1);
                resultMap.put("err_msg", "上传文件异常");
            }
        }
        return resultMap;
    }
    /**
     * 上传用户自定义二维码
     *
     * @param userId
     * @param mediaId
     * @return
     */
    @RequestMapping(value = "/uploadWXFileOfQRcode")
    @ResponseBody
    public Object uploadWXFileOfQRcode(HttpSession session,
                                          @RequestParam("mediaId") String mediaId ) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> resultMap = null;
        UserInfo user = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
        if (null == user) {
            resultMap = new HashMap<String, Object>();
            resultMap.put("err_code", -2);
            resultMap.put("err_msg", "当前没有登录，请重新登录");
            return resultMap;
        }
        try {
            map.put("userId", user.getId());
            map.put("mediaId", mediaId);
            map.put("save_path", ConstantsUtil.getProperty("QRCODE_SAVE_PATH"));
            // 保存文件到服务器，并得到返回状态
            resultMap = weChatFileUploadNo.uploadPmgWXFile(map);
            String path = session.getServletContext().getRealPath(ConstantsUtil.getProperty("QRCODE_SAVE_PATH"));
            System.out.println("--------------------------------------------->path: " + path);

        } catch (Exception ex) {
            if (null == resultMap) {
                resultMap = new HashMap<String, Object>();
                resultMap.put("err_code", -1);
                resultMap.put("err_msg", "上传文件异常");
            }
        }
        return resultMap;
    }

    /**
     * 上传文件
     * @param mediaId
     * @return
     */
    @RequestMapping(value = "/uploadWXFileByMediaId")
    @ResponseBody
    public Object uploadWXFileByMediaId(HttpSession session,
                               @RequestParam("mediaId") String mediaId) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> resultMap = null;
        map.put("mediaId", mediaId);
        resultMap =  weChatFileUploadNo.uploadWXFile(map);
        return resultMap;
    }
}
